<template>
    <div>
        <el-row>
            <el-col>
                <el-form label-width="120px" :model="FinanceLoanDB" :rules="rules" ref="FinanceLoanDBref">
                    <el-row>
                        <el-col :offset="2" :span="9">
                            <el-form-item label="报销类别">
                                <el-input v-model="FinanceLoanDB.Type" ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :offset="2" :span="9">
                            <el-form-item label="申请人"  prop="Borrower">
                                <el-select width="100%" v-model="FinanceLoanDB.Borrower" filterable placeholder="请选择申请人" style="width:100%" >
                                    <el-option value="">请选择申请人</el-option>
                                    <el-option value=" ">全部</el-option>
                                    <el-option v-for="(v,k) in AllUsers" :value="v.Id" :data-userid="v.Id" :label="v.UserName" v-text="v.UserName" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-col :offset="2" :span="9">
                            <el-form-item label="项目">
                                <el-select v-model="FinanceLoanDB.Project" filterable placeholder="请选择项目" style="width: 100%">
                                    <el-option :value="0" label="请选择项目">请选择项目</el-option>
                                  <!--  <el-option  :value="0"  label="请选择项目(可以不选)">  </el-option>-->
                                    <el-option v-for="(v,k) in AllProject" :value="v.Id" :data-userid="v.Id" :label="v.ProjectName" v-text="v.ProjectName" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :offset="2" :span="9">
                            <el-form-item label="金额">
                                <el-input v-model="FinanceLoanDB.Money"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-col :offset="2" :span="9">
                            <el-form-item label="报销内容">
                                <el-input v-model="FinanceLoanDB.Content"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :offset="2" :span="9">
                            <el-form-item label="内容摘要">
                                <el-input v-model="FinanceLoanDB.Abstract"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-col :span="20" :offset="2">
                            <el-form-item label="备注">
                                <el-input v-model="FinanceLoanDB.Remark"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-col :span="20" :offset="2">
                            <el-form-item label="图片上传">
                                <upload-file ref="upload_file_refs" style="display: inline-block;margin-right: 10px;display: block"
                                             content="图片选择"
                                             list_type="picture-card"
                                             btnsize=""
                                             :another-data="AnotherData"
                                             :showfilelist="true"
                                              >
                                </upload-file>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row :gutter="20" style="text-align: center">
                        <input type="hidden" id="Id" />
                        <el-button type="success" @click="SaveAddFinanceReimbursement()">添加 </el-button>
                    </el-row>
                </el-form>
            </el-col>
        </el-row>
        <div>
            <el-dialog :visible.sync="AddFinanceLoandetailsShow" width="60%" title="新建单据明细" :append-to-body="true">
                <AddFinanceLoandetails></AddFinanceLoandetails>
            </el-dialog>

            <el-dialog :visible.sync="UpFinanceLoandetailsShow" width="60%" title="修改单据明细" :append-to-body="true">
                <UpFinanceLoandetails :form="form"></UpFinanceLoandetails>
            </el-dialog>
        </div>
    </div>
</template>

<script>

    import AddFinanceLoandetails from "../FinanceLoan/FinanceLoandetails/AddFinanceLoandetails";
    import UpFinanceLoandetails from "../FinanceLoan/FinanceLoandetails/UpFinanceLoandetails";
    import UploadFile from "../../UploadFile";
    export default {
        name: "AddFinanceReimbursement",
        components: {
            'AddFinanceLoandetails': AddFinanceLoandetails,
            'UpFinanceLoandetails': UpFinanceLoandetails,
            "upload-file": UploadFile,
        },
        props: {
            GetPayArray: {
                type: Array,
                required: true
            },
            AllUsers: {
                type: Array,
                required: true
            },
            AllProject: {
                type: Array,
                required: true
            },
            AllDepartment: {
                type: Array,
                required: true
            },
            AllCategory: {
                type: Array,
                required: true
            },
        },
        created() {
            //this.editPayeeArray = this.editPayee;
        },
        data() {
            return {
                objcss: {
                    display: "none",
                },
                AddFinanceLoandetailsShow: false,
                UpFinanceLoandetailsShow: false,
                src: '',
                detailed: [],
                form: {
                    Purpose: "",
                    Money: '',
                    CategoryTwo: '',
                    CategoryOne: '',
                    HasInvoice: false,
                },
                FinanceLoanDB: {
                    FinanceType: 4,
                    Borrower: '',
                    Department: '',
                    Project: 0,
                    TravelType: 0,
                    TravelPeople: '',
                    TravelAddress: '',
                    Money: '',
                    Remark: '',
                    LoanNumber: '',
                    TravelStartTime: '',
                    TravelEndTime: '',
                    editPayeeArray:[]
                },
                //文件上传参数
                AnotherData: {
                    FileType: 512,
                    id: 1,
                    Type: 0,
                    Single: false,
                },
                rules: {
                    Borrower: [
                        { required: true, message: '请选择您的选项', trigger: 'change' },
                    ],
                    Department: [
                        { required: true, message: '请选择您的选项', trigger: 'change' },
                    ],
                    editPayeeArray: [
                        { type: 'array', required: true, message: '请选择您的选项', trigger: 'change' }
                    ],
                }
            }
        },
        methods: {
            FinanceTypechange(val) {
                if (val != 8) {
                    this.objcss.display = "none";
                }
                else {
                    this.objcss.display = "block";
                }
            },
            GetNameById(id) {
                for (var i = 0; i < this.AllCategory.length; i++) {
                    let Id = parseInt(id)
                    if (Id == this.AllCategory[i].Id) {
                        return this.AllCategory[i].CategoryName;
                    }
                }
            },

            AddFinanceLoandetailsShow_event() {
                this.AddFinanceLoandetailsShow = true;
            },

            _GetAddAllMoney() {
                let sum = 0;
                for (var i = 0; i < this.detailed.length; i++) {
                    sum += parseFloat(this.detailed[i].Money);
                }
                this.FinanceLoanDB.Money = sum.toFixed(2);
            },
            DelAddLoandetails(row, index) {
                this.detailed.splice(index, 1)
                this._GetAddAllMoney();
            },
            GetFinance(row) {
                this.form = row;
                this.UpFinanceLoandetailsShow = true;
            },
            InsertFinance(finance) {
                var isChecked = this.form.HasInvoice;
                if (value.LoanNumber != null && isChecked) {

                    this.$confirm('请再次确认是否为最后一次冲账，点击确定按钮后，后续将不能再对此单据进行冲账！', '警告，请您仔细阅读', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消'
                    }).then(() => {
                        this.SaveAddFinanceReimbursement(finance);
                    })
                }
                else {
                    this.SaveAddFinanceReimbursement(finance);
                }
            },
            SaveAddFinanceReimbursement() {
                this.$refs["FinanceLoanDBref"].validate((valid) => {
                    if (valid) {
                        this.FinanceLoanDB.PayeeType = this.FinanceLoanDB.editPayeeArray[0];
                        this.FinanceLoanDB.Payee = this.FinanceLoanDB.editPayeeArray[1];
                        if (this.FinanceLoanDB.TravelPeople.length > 0) {
                            this.FinanceLoanDB.TravelPeople = this.FinanceLoanDB.TravelPeople.join(",");
                        } else {
                            this.FinanceLoanDB.TravelPeople = "";
                        }
                        if (this.FinanceLoanDB.Department == "")
                            this.FinanceLoanDB.Department = 0;
                        if (this.FinanceLoanDB.Project == "")
                            this.FinanceLoanDB.Project = 0;
                        let loadding = this.openLoading();
                        this.$http.post("/Finance/AddFinanceReimbursement", {
                                finance: this.FinanceLoanDB,
                                financeDocDetails: this.detailed,
                                IsFinishReversal: this.form.HasInvoice,
                            }).then(res => {
                            loadding.close();
                            if (res.data.Id != null) {
                                if (this.$refs['upload_file_refs'].fileList.length > 0) {
                                    this.AnotherData.id = res.data.Id;
                                    this.$refs['upload_file_refs'].submitUpload()
                                }
                                this.$message.success("添加成功")
                                this.$parent.$parent.AddFinanceReimbursementShow = false;
                                this.$parent.$parent.GetFinanceReimbursementDetail();
                            } else {
                                this.$message.error("添加失败")
                            }
                        }).catch(res => {
                            this.$message.error("服务器添加异常")
                            loadding.close();
                        });
                    }
                })
            },
        },
    }
</script>

<style scoped>
    .el-table th {
        padding: 0px;
    }

    .el-select-dropdown__item {
        width: 270px !important;
    }
</style>
